/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.nju.software.greencreditsystem.action;

import cn.edu.nju.software.greencreditsystem.domain.Enterprise;
import cn.edu.nju.software.greencreditsystem.domain.RepaymentRecord;
import cn.edu.nju.software.greencreditsystem.factory.ServiceFactory;
import cn.edu.nju.software.greencreditsystem.service.RepaymentRecordService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import org.apache.struts2.util.ServletContextAware;

/**
 * 作者：韩天
 * 创建时间：2011-8-13
 * 类描述：企业客户管理模块的action封装类
 */
public class EnterpriseCustomerAction extends ActionSupport implements ServletContextAware {
    private Enterprise enterprise;//企业实例
    private double totalAmount;//企业贷款总额
    private double paidAmount;//企业已换总额
    private double paidPercent;//企业已还比例
    private int leftDays;//据下次还款天数
    private RepaymentRecord record;//企业还款记录
    private List<RepaymentRecord> recordList;//企业还款记录
    private BufferedImage image;//某个财务指标的折线图
    private File file;//临时图片文件
    private ServletContext context;
    
    
    @Override
    /**
     * 方法描述：默认的执行方法，实现无条件跳转
     * param：
     * return：SUCCESS
     */
    public String execute() {
        return SUCCESS;
    }
    
    /**
     * 方法描述：显示还贷情况界面
     * param：
     * return：SUCCESS
     */
    public String show_repayment(){
        //取得请求相关的ActionContext实例  
        ActionContext actionContext = ActionContext.getContext();
        Map session = actionContext.getSession();
        int enterpriseId = Integer.valueOf((String) session.get("userId"));
        this.enterprise = new Enterprise();
        this.enterprise.setId(enterpriseId);
        
        RepaymentRecordService rrs = ServiceFactory.getRepaymentRecordServiceInstance();
        this.totalAmount = rrs.getTotalAmount(enterprise.getId());
        this.paidAmount = rrs.getPaidAmount(enterprise.getId());
        this.paidPercent = this.paidAmount*100/this.totalAmount;
        //取得下次还款信息
        this.record = rrs.getNextUnpaid(enterprise.getId());
        //计算据下次还款天数
        Date today = new Date(System.currentTimeMillis());
        this.recordList = rrs.getRecordList(enterprise.getId());
        long mm = 1000;
        this.leftDays = (int)((long)(record.getPlanDate().getTime()-today.getTime())/(mm*60*60*24));
        
        //生成还贷计划图片
        this.image = rrs.get_RepaymentImage(enterpriseId);
        if(file != null){
            file.delete();
        }else{
            file = new File(context.getRealPath("/enterprise/image"), "temp"+enterpriseId+".png");
        }
        try {
            ImageIO.write(image, "png", file);
            image.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        return SUCCESS;        
    }
    
    

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public double getPaidAmount() {
        return paidAmount;
    }

    public void setPaidAmount(double paidAmount) {
        this.paidAmount = paidAmount;
    }

    public double getPaidPercent() {
        return paidPercent;
    }

    public void setPaidPercent(double paidPercent) {
        this.paidPercent = paidPercent;
    }

    public RepaymentRecord getRecord() {
        return record;
    }

    public void setRecord(RepaymentRecord record) {
        this.record = record;
    }

    public List<RepaymentRecord> getRecordList() {
        return recordList;
    }

    public void setRecordList(List<RepaymentRecord> recordList) {
        this.recordList = recordList;
    }

    public double getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(double totalAmount) {
        this.totalAmount = totalAmount;
    }

    public int getLeftDays() {
        return leftDays;
    }

    public void setLeftDays(int leftDays) {
        this.leftDays = leftDays;
    }

    @Override
    public void setServletContext(ServletContext sc) {
        this.context = sc;
    }

    public ServletContext getContext() {
        return context;
    }

    public void setContext(ServletContext context) {
        this.context = context;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public BufferedImage getImage() {
        return image;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }
}
